package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.utils.CommonUtil;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.bo.ZsyyCommunityUserEditBo;
import com.ruoyi.system.domain.ZsyyCommunityUser;
import com.ruoyi.system.mapper.ZsyyCommunityUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.bo.ZsyyCommunityMonthFeeAddBo;
import com.ruoyi.system.bo.ZsyyCommunityMonthFeeQueryBo;
import com.ruoyi.system.bo.ZsyyCommunityMonthFeeEditBo;
import com.ruoyi.system.domain.ZsyyCommunityMonthFee;
import com.ruoyi.system.mapper.ZsyyCommunityMonthFeeMapper;
import com.ruoyi.system.vo.ZsyyCommunityMonthFeeVo;
import com.ruoyi.system.service.IZsyyCommunityMonthFeeService;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 物业缴费记录 Service业务层处理
 *
 * @author wangghua
 * @date 2021-07-14
 */
@Service
public class ZsyyCommunityMonthFeeServiceImpl extends ServicePlusImpl<ZsyyCommunityMonthFeeMapper, ZsyyCommunityMonthFee> implements IZsyyCommunityMonthFeeService {
    @Autowired
    private ZsyyCommunityMonthFeeMapper iZsyyCommunityMonthFeeMapper;
    @Autowired
    private ZsyyCommunityUserMapper communityUserMapper;
	@Autowired
	private GetCommunityIdImpl getCommunityIdImpl;

    @Override
    public ZsyyCommunityMonthFeeVo queryById(Long id){
        return getVoById(id, ZsyyCommunityMonthFeeVo.class);
    }

    @Override
    public TableDataInfo<ZsyyCommunityMonthFeeVo> queryPageList(ZsyyCommunityMonthFeeQueryBo bo) {
//		bo.setCommunityId(getCommunityIdImpl.getLoginUser().getCommunityId());
		Page<ZsyyCommunityMonthFeeVo> result = iZsyyCommunityMonthFeeMapper.queryPageList(PageUtils.buildPage(),bo);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<ZsyyCommunityMonthFeeVo> queryList(ZsyyCommunityMonthFeeQueryBo bo) {
        return iZsyyCommunityMonthFeeMapper.queryList(bo);
    }

    private LambdaQueryWrapper<ZsyyCommunityMonthFee> buildQueryWrapper(ZsyyCommunityMonthFeeQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZsyyCommunityMonthFee> lqw = Wrappers.lambdaQuery();
		bo.setCommunityId(getCommunityIdImpl.getLoginUser().getCommunityId());
        lqw.eq(bo.getCommunityId() != null, ZsyyCommunityMonthFee::getCommunityId, bo.getCommunityId());
        lqw.eq(StrUtil.isNotBlank(bo.getFeeNo()), ZsyyCommunityMonthFee::getFeeNo, bo.getFeeNo());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(ZsyyCommunityUserEditBo bo) {
		ZsyyCommunityUser communityUser = communityUserMapper.selectById(bo.getId());
		communityUser.setFeeStatus(1);//业主已缴纳物业费
		communityUser.setEndtime(bo.getValidityPeriod());
		communityUserMapper.updateById(communityUser);

		ZsyyCommunityMonthFee monthFee = new ZsyyCommunityMonthFee();
		monthFee.setCommunityId(bo.getCommunityId());
		monthFee.setFeeNo(CommonUtil.getFeeNoByUUId());
		monthFee.setCreatetime(new Date());
		monthFee.setOwnerId(bo.getId());
		monthFee.setPayPrice(bo.getPayPrice());
		monthFee.setRemark(bo.getRemark());
		monthFee.setValidityPeriod(bo.getValidityPeriod());
        return save(monthFee);
    }

    @Override
    public Boolean updateByEditBo(ZsyyCommunityMonthFeeEditBo bo) {
        ZsyyCommunityMonthFee update = BeanUtil.toBean(bo, ZsyyCommunityMonthFee.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(ZsyyCommunityMonthFee entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
